package com.haibei.adapter;

import java.util.ArrayList;
import java.util.HashSet;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.cache.SimpleImgLoaderLru;
import com.haibei.R;
import com.haibei.adapter.bean.CollectTopic;
import com.haibei.adapter.bean.Topic;

public class CollectTopicAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<CollectTopic> mArrayListDataList;
	private boolean isEdit = false;
	private HashSet<CollectTopic> mHashSetSelect;

	public CollectTopicAdapter(Activity activity, ArrayList<CollectTopic> datas) {
		this.activity = activity;
		this.mArrayListDataList = datas;
		this.mHashSetSelect = new HashSet<CollectTopic>();
	}

	public HashSet<CollectTopic> getSelectSet() {
		return mHashSetSelect;
	}

	public void removeSelect() {
		ArrayList<CollectTopic> needDletes = new ArrayList<CollectTopic>();
		for (CollectTopic topic : mArrayListDataList) {
			if (mHashSetSelect.contains(topic)) {
				needDletes.add(topic);
			}
		}
		mArrayListDataList.removeAll(needDletes);
		mHashSetSelect.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mArrayListDataList.size();
	}

	@Override
	public Object getItem(int position) {
		return mArrayListDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vl;
		if (null == convertView) {
			convertView = View.inflate(activity, R.layout.item_collect_topic, null);
			vl = new ViewHolder();
			vl.textViewTitle = (TextView) convertView.findViewById(R.id.textViewTitle);
			vl.textLead = (TextView) convertView.findViewById(R.id.textViewLead);
			vl.textViewComments = (TextView) convertView.findViewById(R.id.textViewCommentCount);
			vl.textPeroid = (TextView) convertView.findViewById(R.id.textViewPeriod);
			vl.imageViewCover = (ImageView) convertView.findViewById(R.id.imageViewImg);
			vl.relativeLayoutRadio = convertView.findViewById(R.id.relativeLayoutRadio);
			vl.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxDel);
			convertView.setTag(vl);
		} else {
			vl = (ViewHolder) convertView.getTag();
		}
		Topic topic = mArrayListDataList.get(position);
		vl.textViewTitle.setText(topic.getTitle());
		vl.textLead.setText(topic.getLead());
		vl.textViewComments.setText(topic.getConmentCount() + "评论");
		vl.textPeroid.setText("第" + topic.getPeriod() + "期");
		final CollectTopic dataBean = mArrayListDataList.get(position);

		if (isEdit) {
			vl.relativeLayoutRadio.setVisibility(View.VISIBLE);
			vl.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						mHashSetSelect.add(dataBean);
					} else {
						mHashSetSelect.remove(dataBean);
					}
				}
			});
			if (mHashSetSelect.contains(dataBean)) {
				vl.checkBox.setChecked(true);
			}
		} else {
			vl.relativeLayoutRadio.setVisibility(View.GONE);
			vl.checkBox.setOnCheckedChangeListener(null);
		}
		SimpleImgLoaderLru.from(activity).displayImage(vl.imageViewCover, topic.getCoverImg(), R.drawable.bg_default_consult);
		return convertView;
	}

	public void setEditEable(boolean isEdit) {
		this.isEdit = isEdit;
		notifyDataSetChanged();
	}

	public boolean isEdit() {
		return isEdit;
	}

	class ViewHolder {
		TextView textViewTitle;
		TextView textLead;
		TextView textViewComments;
		TextView textPeroid;
		ImageView imageViewCover;
		View relativeLayoutRadio;
		CheckBox checkBox;
	}

}
